emqx_dashboard_error_code_SUITE.erl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. -define(SERVER, "http://127.0.0.1:18083/api/v5").
  22. all() ->
  23. emqx_common_test_helpers:all(?MODULE).
  24. init_per_suite(Config) ->
  25. emqx_mgmt_api_test_util:init_suite([emqx_conf]),
  26. Config.
  27. end_per_suite(_Config) ->
  28. emqx_mgmt_api_test_util:end_suite([emqx_conf]).
  29. t_all_code(_) ->
  30. HrlDef = ?ERROR_CODES,
  31. All = emqx_dashboard_error_code:all(),
  32. ?assertEqual(length(HrlDef), length(All)),
  33. ok.
  34. t_list_code(_) ->
  35. List = emqx_dashboard_error_code:list(),
  36. [?assert(exist(CodeName, List)) || CodeName <- emqx_dashboard_error_code:all()],
  37. ok.
  38. t_look_up_code(_) ->
  39. Fun =
  40. fun(CodeName) ->
  41. {ok, _} = emqx_dashboard_error_code:look_up(CodeName)
  42. end,
  43. lists:foreach(Fun, emqx_dashboard_error_code:all()),
  44. {error, not_found} = emqx_dashboard_error_code:look_up('_____NOT_EXIST_NAME'),
  45. ok.
  46. t_description_code(_) ->
  47. {error, not_found} = emqx_dashboard_error_code:description('_____NOT_EXIST_NAME'),
  48. {ok, <<"Request parameters are invalid">>} =
  49. emqx_dashboard_error_code:description('BAD_REQUEST'),
  50. ok.
  51. t_format_code(_) ->
  52. #{code := 'TEST_SUITE_CODE', description := <<"for test suite">>} =
  53. emqx_dashboard_error_code:format({'TEST_SUITE_CODE', <<"for test suite">>}),
  54. ok.
  55. t_api_codes(_) ->
  56. Url = ?SERVER ++ "/error_codes",
  57. {ok, List} = request(Url),
  58. [
  59. ?assert(exist(atom_to_binary(CodeName, utf8), List))
  60. || CodeName <- emqx_dashboard_error_code:all()
  61. ],
  62. ok.
  63. t_api_code(_) ->
  64. Url = ?SERVER ++ "/error_codes/BAD_REQUEST",
  65. {ok, #{
  66. <<"code">> := <<"BAD_REQUEST">>,
  67. <<"description">> := <<"Request parameters are invalid">>
  68. }} = request(Url),
  69. ok.
  70. exist(_CodeName, []) ->
  71. false;
  72. exist(CodeName, [#{code := CodeName, description := _} | _List]) ->
  73. true;
  74. exist(CodeName, [#{<<"code">> := CodeName, <<"description">> := _} | _List]) ->
  75. true;
  76. exist(CodeName, [_ | List]) ->
  77. exist(CodeName, List).
  78. request(Url) ->
  79. Request = {Url, []},
  80. case httpc:request(get, Request, [], []) of
  81. {error, Reason} ->
  82. {error, Reason};
  83. {ok, {{"HTTP/1.1", Code, _}, _, Return}} when
  84. Code >= 200 andalso Code =< 299
  85. ->
  86. {ok, emqx_utils_json:decode(Return, [return_maps])};
  87. {ok, {Reason, _, _}} ->
  88. {error, Reason}
  89. end.