emqx_dashboard_error_code_SUITE.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. %%
  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. mria:start(),
  26. application:load(emqx_dashboard),
  27. emqx_common_test_helpers:start_apps([emqx_conf, emqx_dashboard], fun set_special_configs/1),
  28. Config.
  29. set_special_configs(emqx_dashboard) ->
  30. emqx_dashboard_api_test_helpers:set_default_config(),
  31. ok;
  32. set_special_configs(_) ->
  33. ok.
  34. end_per_suite(Config) ->
  35. end_suite(),
  36. Config.
  37. end_suite() ->
  38. application:unload(emqx_management),
  39. emqx_common_test_helpers:stop_apps([emqx_dashboard]).
  40. t_all_code(_) ->
  41. HrlDef = ?ERROR_CODES,
  42. All = emqx_dashboard_error_code:all(),
  43. ?assertEqual(length(HrlDef), length(All)),
  44. ok.
  45. t_list_code(_) ->
  46. List = emqx_dashboard_error_code:list(),
  47. [?assert(exist(CodeName, List)) || CodeName <- emqx_dashboard_error_code:all()],
  48. ok.
  49. t_look_up_code(_) ->
  50. Fun =
  51. fun(CodeName) ->
  52. {ok, _} = emqx_dashboard_error_code:look_up(CodeName)
  53. end,
  54. lists:foreach(Fun, emqx_dashboard_error_code:all()),
  55. {error, not_found} = emqx_dashboard_error_code:look_up('_____NOT_EXIST_NAME'),
  56. ok.
  57. t_description_code(_) ->
  58. {error, not_found} = emqx_dashboard_error_code:description('_____NOT_EXIST_NAME'),
  59. {ok, <<"Request parameters are not legal">>} =
  60. emqx_dashboard_error_code:description('BAD_REQUEST'),
  61. ok.
  62. t_format_code(_) ->
  63. #{code := 'TEST_SUITE_CODE', description := <<"for test suite">>} =
  64. emqx_dashboard_error_code:format({'TEST_SUITE_CODE', <<"for test suite">>}),
  65. ok.
  66. t_api_codes(_) ->
  67. Url = ?SERVER ++ "/error_codes",
  68. {ok, List} = request(Url),
  69. [
  70. ?assert(exist(atom_to_binary(CodeName, utf8), List))
  71. || CodeName <- emqx_dashboard_error_code:all()
  72. ],
  73. ok.
  74. t_api_code(_) ->
  75. Url = ?SERVER ++ "/error_codes/BAD_REQUEST",
  76. {ok, #{
  77. <<"code">> := <<"BAD_REQUEST">>,
  78. <<"description">> := <<"Request parameters are not legal">>
  79. }} = request(Url),
  80. ok.
  81. exist(_CodeName, []) ->
  82. false;
  83. exist(CodeName, [#{code := CodeName, description := _} | _List]) ->
  84. true;
  85. exist(CodeName, [#{<<"code">> := CodeName, <<"description">> := _} | _List]) ->
  86. true;
  87. exist(CodeName, [_ | List]) ->
  88. exist(CodeName, List).
  89. request(Url) ->
  90. Request = {Url, []},
  91. case httpc:request(get, Request, [], []) of
  92. {error, Reason} ->
  93. {error, Reason};
  94. {ok, {{"HTTP/1.1", Code, _}, _, Return}} when
  95. Code >= 200 andalso Code =< 299
  96. ->
  97. {ok, emqx_json:decode(Return, [return_maps])};
  98. {ok, {Reason, _, _}} ->
  99. {error, Reason}
  100. end.