emqx_authz_api_cache_SUITE.erl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  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. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. %%--------------------------------------------------------------------
  15. -module(emqx_authz_api_cache_SUITE).
  16. -compile(nowarn_export_all).
  17. -compile(export_all).
  18. -import(emqx_dashboard_api_test_helpers, [request/2, uri/1]).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. suite() -> [{timetrap, {seconds, 60}}].
  22. all() ->
  23. emqx_common_test_helpers:all(?MODULE).
  24. groups() ->
  25. [].
  26. init_per_suite(Config) ->
  27. ok = emqx_common_test_helpers:start_apps(
  28. [emqx_conf, emqx_authz, emqx_dashboard, emqx_management],
  29. fun set_special_configs/1
  30. ),
  31. Config.
  32. end_per_suite(_Config) ->
  33. {ok, _} = emqx:update_config(
  34. [authorization],
  35. #{
  36. <<"no_match">> => <<"allow">>,
  37. <<"cache">> => #{<<"enable">> => <<"true">>},
  38. <<"sources">> => []
  39. }
  40. ),
  41. emqx_common_test_helpers:stop_apps([emqx_dashboard, emqx_authz, emqx_conf, emqx_management]),
  42. ok.
  43. set_special_configs(emqx_dashboard) ->
  44. emqx_dashboard_api_test_helpers:set_default_config();
  45. set_special_configs(emqx_authz) ->
  46. {ok, _} = emqx:update_config([authorization, cache, enable], true),
  47. {ok, _} = emqx:update_config([authorization, no_match], deny),
  48. {ok, _} = emqx:update_config([authorization, sources], []),
  49. ok;
  50. set_special_configs(_App) ->
  51. ok.
  52. t_clean_cahce(_) ->
  53. {ok, C} = emqtt:start_link([{clientid, <<"emqx0">>}, {username, <<"emqx0">>}]),
  54. {ok, _} = emqtt:connect(C),
  55. {ok, _, _} = emqtt:subscribe(C, <<"a/b/c">>, 0),
  56. ok = emqtt:publish(C, <<"a/b/c">>, <<"{\"x\":1,\"y\":1}">>, 0),
  57. {ok, 200, Result3} = request(get, uri(["clients", "emqx0", "authorization", "cache"])),
  58. ?assertEqual(2, length(jsx:decode(Result3))),
  59. request(delete, uri(["authorization", "cache"])),
  60. {ok, 200, Result4} = request(get, uri(["clients", "emqx0", "authorization", "cache"])),
  61. ?assertEqual(0, length(jsx:decode(Result4))),
  62. ok.
  63. stop_apps(Apps) ->
  64. lists:foreach(fun application:stop/1, Apps).