emqx_authz_api_settings_SUITE.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_settings_SUITE).
  16. -compile(nowarn_export_all).
  17. -compile(export_all).
  18. -include("emqx_authz.hrl").
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. -define(HOST, "http://127.0.0.1:18083/").
  22. -define(API_VERSION, "v5").
  23. -define(BASE_PATH, "api").
  24. all() ->
  25. emqx_common_test_helpers:all(?MODULE).
  26. groups() ->
  27. [].
  28. init_per_suite(Config) ->
  29. ok = emqx_common_test_helpers:start_apps(
  30. [emqx_conf, emqx_authz, emqx_dashboard],
  31. fun set_special_configs/1),
  32. Config.
  33. end_per_suite(_Config) ->
  34. {ok, _} = emqx:update_config(
  35. [authorization],
  36. #{<<"no_match">> => <<"allow">>,
  37. <<"cache">> => #{<<"enable">> => <<"true">>},
  38. <<"sources">> => []}),
  39. emqx_common_test_helpers:stop_apps([emqx_dashboard, emqx_authz, emqx_conf]),
  40. ok.
  41. set_special_configs(emqx_dashboard) ->
  42. Config = #{
  43. default_username => <<"admin">>,
  44. default_password => <<"public">>,
  45. listeners => [#{
  46. protocol => http,
  47. port => 18083
  48. }]
  49. },
  50. emqx_config:put([emqx_dashboard], Config),
  51. ok;
  52. set_special_configs(emqx_authz) ->
  53. {ok, _} = emqx:update_config([authorization, cache, enable], false),
  54. {ok, _} = emqx:update_config([authorization, no_match], deny),
  55. {ok, _} = emqx:update_config([authorization, sources], []),
  56. ok;
  57. set_special_configs(_App) ->
  58. ok.
  59. %%------------------------------------------------------------------------------
  60. %% Testcases
  61. %%------------------------------------------------------------------------------
  62. t_api(_) ->
  63. Settings1 = #{<<"no_match">> => <<"deny">>,
  64. <<"deny_action">> => <<"disconnect">>,
  65. <<"cache">> => #{
  66. <<"enable">> => false,
  67. <<"max_size">> => 32,
  68. <<"ttl">> => 60000
  69. }
  70. },
  71. {ok, 200, Result1} = request(put, uri(["authorization", "settings"]), Settings1),
  72. {ok, 200, Result1} = request(get, uri(["authorization", "settings"]), []),
  73. ?assertEqual(Settings1, jsx:decode(Result1)),
  74. Settings2 = #{<<"no_match">> => <<"allow">>,
  75. <<"deny_action">> => <<"ignore">>,
  76. <<"cache">> => #{
  77. <<"enable">> => true,
  78. <<"max_size">> => 32,
  79. <<"ttl">> => 60000
  80. }
  81. },
  82. {ok, 200, Result2} = request(put, uri(["authorization", "settings"]), Settings2),
  83. {ok, 200, Result2} = request(get, uri(["authorization", "settings"]), []),
  84. ?assertEqual(Settings2, jsx:decode(Result2)),
  85. ok.
  86. %%--------------------------------------------------------------------
  87. %% HTTP Request
  88. %%--------------------------------------------------------------------
  89. request(Method, Url, Body) ->
  90. Request = case Body of
  91. [] -> {Url, [auth_header_()]};
  92. _ -> {Url, [auth_header_()], "application/json", jsx:encode(Body)}
  93. end,
  94. ct:pal("Method: ~p, Request: ~p", [Method, Request]),
  95. case httpc:request(Method, Request, [], [{body_format, binary}]) of
  96. {error, socket_closed_remotely} ->
  97. {error, socket_closed_remotely};
  98. {ok, {{"HTTP/1.1", Code, _}, _Headers, Return} } ->
  99. {ok, Code, Return};
  100. {ok, {Reason, _, _}} ->
  101. {error, Reason}
  102. end.
  103. uri() -> uri([]).
  104. uri(Parts) when is_list(Parts) ->
  105. NParts = [E || E <- Parts],
  106. ?HOST ++ filename:join([?BASE_PATH, ?API_VERSION | NParts]).
  107. get_sources(Result) ->
  108. maps:get(<<"sources">>, jsx:decode(Result), []).
  109. auth_header_() ->
  110. Username = <<"admin">>,
  111. Password = <<"public">>,
  112. {ok, Token} = emqx_dashboard_admin:sign_token(Username, Password),
  113. {"Authorization", "Bearer " ++ binary_to_list(Token)}.