emqx_authz_api_settings_SUITE.erl 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 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_settings_SUITE).
  16. -compile(nowarn_export_all).
  17. -compile(export_all).
  18. -import(emqx_mgmt_api_test_util, [request/3, 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_mgmt_api_test_util:init_suite(
  27. [emqx_conf, emqx_authz, emqx_dashboard],
  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]),
  41. emqx_mgmt_api_test_util:end_suite([emqx_authz, emqx_conf]),
  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], false),
  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. %%------------------------------------------------------------------------------
  53. %% Testcases
  54. %%------------------------------------------------------------------------------
  55. t_api(_) ->
  56. Settings1 = #{
  57. <<"no_match">> => <<"deny">>,
  58. <<"deny_action">> => <<"disconnect">>,
  59. <<"cache">> => #{
  60. <<"enable">> => false,
  61. <<"max_size">> => 32,
  62. <<"ttl">> => <<"60s">>
  63. }
  64. },
  65. {ok, 200, Result1} = request(put, uri(["authorization", "settings"]), Settings1),
  66. {ok, 200, Result1} = request(get, uri(["authorization", "settings"]), []),
  67. ?assertEqual(Settings1, emqx_utils_json:decode(Result1)),
  68. Settings2 = #{
  69. <<"no_match">> => <<"allow">>,
  70. <<"deny_action">> => <<"ignore">>,
  71. <<"cache">> => #{
  72. <<"enable">> => true,
  73. <<"max_size">> => 32,
  74. <<"ttl">> => <<"60s">>
  75. }
  76. },
  77. {ok, 200, Result2} = request(put, uri(["authorization", "settings"]), Settings2),
  78. {ok, 200, Result2} = request(get, uri(["authorization", "settings"]), []),
  79. ?assertEqual(Settings2, emqx_utils_json:decode(Result2)),
  80. ok.
  81. stop_apps(Apps) ->
  82. lists:foreach(fun application:stop/1, Apps).