emqx_authz_api_cache_SUITE.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. all() ->
  22. emqx_common_test_helpers:all(?MODULE).
  23. groups() ->
  24. [].
  25. init_per_suite(Config) ->
  26. ok = emqx_common_test_helpers:start_apps(
  27. [emqx_conf, emqx_authz, emqx_dashboard, emqx_management],
  28. fun set_special_configs/1
  29. ),
  30. Config.
  31. end_per_suite(_Config) ->
  32. {ok, _} = emqx:update_config(
  33. [authorization],
  34. #{
  35. <<"no_match">> => <<"allow">>,
  36. <<"cache">> => #{<<"enable">> => <<"true">>},
  37. <<"sources">> => []
  38. }
  39. ),
  40. ok = stop_apps([emqx_resource, emqx_connector]),
  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).