1: %%==============================================================================
    2: %% Copyright 2012 Erlang Solutions Ltd.
    3: %%
    4: %% Licensed under the Apache License, Version 2.0 (the "License");
    5: %% you may not use this file except in compliance with the License.
    6: %% You may obtain a copy of the License at
    7: %%
    8: %% http://www.apache.org/licenses/LICENSE-2.0
    9: %%
   10: %% Unless required by applicable law or agreed to in writing, software
   11: %% distributed under the License is distributed on an "AS IS" BASIS,
   12: %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13: %% See the License for the specific language governing permissions and
   14: %% limitations under the License.
   15: %%==============================================================================
   16: -module(adhoc_SUITE).
   17: -compile([export_all, nowarn_export_all]).
   18: 
   19: -include_lib("escalus/include/escalus.hrl").
   20: -include_lib("common_test/include/ct.hrl").
   21: -include_lib("eunit/include/eunit.hrl").
   22: 
   23: -import(domain_helper, [host_type/0, domain/0]).
   24: 
   25: %%--------------------------------------------------------------------
   26: %% Suite configuration
   27: %%--------------------------------------------------------------------
   28: 
   29: -define(NS_COMMANDS, <<"http://jabber.org/protocol/commands">>).
   30: 
   31: all() ->
   32:     [{group, disco_visible},
   33:      {group, adhoc}].
   34: 
   35: groups() ->
   36:     [{adhoc, [parallel], common_disco_cases() ++ hidden_disco_cases() ++ [ping]},
   37:          {disco_visible, [parallel], common_disco_cases() ++ visible_disco_cases()}].
   38: 
   39: common_disco_cases() ->
   40:     [disco_info,
   41:      disco_info_sm,
   42:      disco_info_commands,
   43:      disco_info_sm_commands,
   44:      disco_info_ping,
   45:      disco_items_commands].
   46: 
   47: hidden_disco_cases() ->
   48:     [disco_items_hidden,
   49:      disco_items_sm_hidden].
   50: 
   51: visible_disco_cases() ->
   52:     [disco_items_visible,
   53:      disco_items_sm_visible].
   54: 
   55: suite() ->
   56:     escalus:suite().
   57: 
   58: init_per_suite(Config) ->
   59:     escalus:init_per_suite(Config).
   60: 
   61: end_per_suite(Config) ->
   62:     escalus_fresh:clean(),
   63:     escalus:end_per_suite(Config).
   64: 
   65: init_per_group(GroupName, Config) ->
   66:     Config1 = init_modules(GroupName, Config),
   67:     escalus:create_users(Config1, escalus:get_users([alice, bob])).
   68: 
   69: end_per_group(GroupName, Config) ->
   70:     restore_modules(GroupName, Config),
   71:     escalus:delete_users(Config, escalus:get_users([alice, bob])).
   72: 
   73: init_per_testcase(CaseName, Config) ->
   74:     escalus:init_per_testcase(CaseName, Config).
   75: 
   76: end_per_testcase(CaseName, Config) ->
   77:     escalus:end_per_testcase(CaseName, Config).
   78: 
   79: init_modules(disco_visible, Config) ->
   80:     Config1 = escalus:init_per_suite(dynamic_modules:save_modules(host_type(), Config)),
   81:     dynamic_modules:ensure_modules(host_type(), [{mod_adhoc, [{report_commands_node, true}]}]),
   82:     Config1;
   83: init_modules(_, Config) ->
   84:     Config.
   85: 
   86: restore_modules(disco_visible, Config) ->
   87:     dynamic_modules:restore_modules(Config);
   88: restore_modules(_, _Config) ->
   89:     ok.
   90: 
   91: %%--------------------------------------------------------------------
   92: %% Adhoc tests
   93: %%--------------------------------------------------------------------
   94: 
   95: disco_info(Config) ->
   96:     escalus:fresh_story(Config, [{alice, 1}],
   97:         fun(Alice) ->
   98:                 Server = escalus_client:server(Alice),
   99:                 escalus:send(Alice, escalus_stanza:disco_info(Server)),
  100:                 Stanza = escalus:wait_for_stanza(Alice),
  101:                 escalus:assert(has_feature, [?NS_COMMANDS], Stanza),
  102:                 escalus:assert(is_stanza_from, [domain()], Stanza)
  103:         end).
  104: 
  105: disco_info_sm(Config) ->
  106:     escalus:fresh_story(Config, [{alice, 1}],
  107:         fun(Alice) ->
  108:                 AliceJid = escalus_client:short_jid(Alice),
  109:                 escalus:send(Alice, escalus_stanza:disco_info(AliceJid)),
  110:                 Stanza = escalus:wait_for_stanza(Alice),
  111:                 escalus:assert(has_feature, [?NS_COMMANDS], Stanza),
  112:                 escalus:assert(is_stanza_from, [AliceJid], Stanza)
  113:         end).
  114: 
  115: disco_info_commands(Config) ->
  116:     escalus:fresh_story(Config, [{alice, 1}],
  117:         fun(Alice) ->
  118:                 Server = escalus_client:server(Alice),
  119:                 escalus:send(Alice, escalus_stanza:disco_info(Server, ?NS_COMMANDS)),
  120:                 Stanza = escalus:wait_for_stanza(Alice),
  121:                 escalus:assert(has_identity, [<<"automation">>, <<"command-list">>], Stanza),
  122:                 escalus:assert(is_stanza_from, [domain()], Stanza)
  123:         end).
  124: 
  125: disco_info_sm_commands(Config) ->
  126:     escalus:fresh_story(Config, [{alice, 1}],
  127:         fun(Alice) ->
  128:                 AliceJid = escalus_client:short_jid(Alice),
  129:                 escalus:send(Alice, escalus_stanza:disco_info(AliceJid, ?NS_COMMANDS)),
  130:                 Stanza = escalus:wait_for_stanza(Alice),
  131:                 escalus:assert(has_identity, [<<"automation">>, <<"command-list">>], Stanza),
  132:                 escalus:assert(is_stanza_from, [AliceJid], Stanza)
  133:         end).
  134: 
  135: disco_info_ping(Config) ->
  136:     escalus:fresh_story(Config, [{alice, 1}],
  137:         fun(Alice) ->
  138:                 Server = escalus_client:server(Alice),
  139:                 escalus:send(Alice, escalus_stanza:disco_info(Server, <<"ping">>)),
  140:                 Stanza = escalus:wait_for_stanza(Alice),
  141:                 escalus:assert(has_identity, [<<"automation">>, <<"command-node">>], Stanza),
  142:                 escalus:assert(has_feature, [?NS_COMMANDS], Stanza),
  143:                 escalus:assert(is_stanza_from, [domain()], Stanza)
  144:         end).
  145: 
  146: disco_items_hidden(Config) ->
  147:     escalus:fresh_story(Config, [{alice, 1}],
  148:         fun(Alice) ->
  149:                 Server = escalus_client:server(Alice),
  150:                 escalus:send(Alice, escalus_stanza:disco_items(Server)),
  151:                 Stanza = escalus:wait_for_stanza(Alice),
  152:                 Query = exml_query:subelement(Stanza, <<"query">>),
  153:                 ?assertEqual(undefined,
  154:                              exml_query:subelement_with_attr(Query, <<"node">>, ?NS_COMMANDS)),
  155:                 escalus:assert(is_stanza_from, [domain()], Stanza)
  156:         end).
  157: 
  158: disco_items_sm_hidden(Config) ->
  159:     escalus:fresh_story(Config, [{alice, 1}],
  160:         fun(Alice) ->
  161:                 AliceJid = escalus_client:short_jid(Alice),
  162:                 escalus:send(Alice, escalus_stanza:disco_items(AliceJid)),
  163:                 Stanza = escalus:wait_for_stanza(Alice),
  164:                 Query = exml_query:subelement(Stanza, <<"query">>),
  165:                 ?assertEqual(undefined,
  166:                              exml_query:subelement_with_attr(Query, <<"node">>, ?NS_COMMANDS)),
  167:                 escalus:assert(is_stanza_from, [AliceJid], Stanza)
  168:         end).
  169: 
  170: disco_items_visible(Config) ->
  171:     escalus:fresh_story(Config, [{alice, 1}],
  172:         fun(Alice) ->
  173:                 Server = escalus_client:server(Alice),
  174:                 escalus:send(Alice, escalus_stanza:disco_items(Server)),
  175:                 Stanza = escalus:wait_for_stanza(Alice),
  176:                 Query = exml_query:subelement(Stanza, <<"query">>),
  177:                 Item = exml_query:subelement_with_attr(Query, <<"node">>, ?NS_COMMANDS),
  178:                 ?assertEqual(Server, exml_query:attr(Item, <<"jid">>)),
  179:                 escalus:assert(is_stanza_from, [domain()], Stanza)
  180:         end).
  181: 
  182: disco_items_sm_visible(Config) ->
  183:     escalus:fresh_story(Config, [{alice, 1}],
  184:         fun(Alice) ->
  185:                 AliceJid = escalus_client:short_jid(Alice),
  186:                 escalus:send(Alice, escalus_stanza:disco_items(AliceJid)),
  187:                 Stanza = escalus:wait_for_stanza(Alice),
  188:                 Query = exml_query:subelement(Stanza, <<"query">>),
  189:                 Item = exml_query:subelement_with_attr(Query, <<"node">>, ?NS_COMMANDS),
  190:                 ?assertEqual(AliceJid, exml_query:attr(Item, <<"jid">>)),
  191:                 escalus:assert(is_stanza_from, [AliceJid], Stanza)
  192:         end).
  193: 
  194: disco_items_commands(Config) ->
  195:     escalus:fresh_story(Config, [{alice, 1}],
  196:         fun(Alice) ->
  197:                 Server = escalus_client:server(Alice),
  198:                 escalus:send(Alice, escalus_stanza:disco_items(Server, ?NS_COMMANDS)),
  199:                 Stanza = escalus:wait_for_stanza(Alice),
  200:                 Query = exml_query:subelement(Stanza, <<"query">>),
  201:                 Item = exml_query:subelement_with_attr(Query, <<"node">>, <<"ping">>),
  202:                 ?assertEqual(Server, exml_query:attr(Item, <<"jid">>)),
  203:                 escalus:assert(is_stanza_from, [domain()], Stanza)
  204:         end).
  205: 
  206: ping(Config) ->
  207:     escalus:fresh_story(Config, [{alice, 1}],
  208:         fun(Alice) ->
  209:                 %% Alice pings the server using adhoc command
  210:                 escalus_client:send(Alice, escalus_stanza:to(
  211:                                              escalus_stanza:adhoc_request(<<"ping">>),
  212:                                              domain())),
  213:                 %% Server replies to Alice with pong
  214:                 AdHocResp = escalus_client:wait_for_stanza(Alice),
  215:                 escalus:assert(is_adhoc_response, [<<"ping">>, <<"completed">>],
  216:                                AdHocResp)
  217:         end).