emqx_dashboard_error_code_SUITE.erl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 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. %%
  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(emqx_dashboard_error_code_SUITE).
  17. -compile(nowarn_export_all).
  18. -compile(export_all).
  19. -include_lib("emqx/include/http_api.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("common_test/include/ct.hrl").
  22. -define(SERVER, "http://127.0.0.1:18083/api/v5").
  23. all() ->
  24. emqx_common_test_helpers:all(?MODULE).
  25. init_per_suite(Config) ->
  26. Apps = emqx_cth_suite:start(
  27. [
  28. emqx_conf,
  29. emqx_management,
  30. emqx_mgmt_api_test_util:emqx_dashboard()
  31. ],
  32. #{work_dir => emqx_cth_suite:work_dir(Config)}
  33. ),
  34. [{apps, Apps} | Config].
  35. end_per_suite(Config) ->
  36. Apps = ?config(apps, Config),
  37. emqx_cth_suite:stop(Apps),
  38. ok.
  39. t_all_code(_) ->
  40. HrlDef = ?ERROR_CODES,
  41. All = emqx_dashboard_error_code:all(),
  42. ?assertEqual(length(HrlDef), length(All)),
  43. ok.
  44. t_list_code(_) ->
  45. List = emqx_dashboard_error_code:list(),
  46. [?assert(exist(CodeName, List)) || CodeName <- emqx_dashboard_error_code:all()],
  47. ok.
  48. t_look_up_code(_) ->
  49. Fun =
  50. fun(CodeName) ->
  51. {ok, _} = emqx_dashboard_error_code:look_up(CodeName)
  52. end,
  53. lists:foreach(Fun, emqx_dashboard_error_code:all()),
  54. {error, not_found} = emqx_dashboard_error_code:look_up('_____NOT_EXIST_NAME'),
  55. ok.
  56. t_description_code(_) ->
  57. {error, not_found} = emqx_dashboard_error_code:description('_____NOT_EXIST_NAME'),
  58. {ok, <<"Request parameters are invalid">>} =
  59. emqx_dashboard_error_code:description('BAD_REQUEST'),
  60. ok.
  61. t_format_code(_) ->
  62. #{code := 'TEST_SUITE_CODE', description := <<"for test suite">>} =
  63. emqx_dashboard_error_code:format({'TEST_SUITE_CODE', <<"for test suite">>}),
  64. ok.
  65. t_api_codes(_) ->
  66. Url = ?SERVER ++ "/error_codes",
  67. {ok, List} = request(Url),
  68. [
  69. ?assert(exist(atom_to_binary(CodeName, utf8), List))
  70. || CodeName <- emqx_dashboard_error_code:all()
  71. ],
  72. ok.
  73. t_api_code(_) ->
  74. Url = ?SERVER ++ "/error_codes/BAD_REQUEST",
  75. {ok, #{
  76. <<"code">> := <<"BAD_REQUEST">>,
  77. <<"description">> := <<"Request parameters are invalid">>
  78. }} = request(Url),
  79. ok.
  80. exist(_CodeName, []) ->
  81. false;
  82. exist(CodeName, [#{code := CodeName, description := _} | _List]) ->
  83. true;
  84. exist(CodeName, [#{<<"code">> := CodeName, <<"description">> := _} | _List]) ->
  85. true;
  86. exist(CodeName, [_ | List]) ->
  87. exist(CodeName, List).
  88. request(Url) ->
  89. Request = {Url, []},
  90. case httpc:request(get, Request, [], []) of
  91. {error, Reason} ->
  92. {error, Reason};
  93. {ok, {{"HTTP/1.1", Code, _}, _, Return}} when
  94. Code >= 200 andalso Code =< 299
  95. ->
  96. {ok, emqx_utils_json:decode(Return, [return_maps])};
  97. {ok, {Reason, _, _}} ->
  98. {error, Reason}
  99. end.