emqx_dashboard_error_code_SUITE.erl 3.8 KB

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