From 9ef179598580faf3649b83f9aba1247af9448e10 Mon Sep 17 00:00:00 2001 From: DDDHuang <44492639+DDDHuang@users.noreply.github.com> Date: Wed, 13 Apr 2022 14:23:04 +0800 Subject: [PATCH] fix: add bad api test --- .../test/emqx_dashboard_bad_api_SUITE.erl | 74 +++++++++++++++++++ ...ITE.erl => emqx_mgmt_api_topics_SUITE.erl} | 9 ++- 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 apps/emqx_dashboard/test/emqx_dashboard_bad_api_SUITE.erl rename apps/emqx_management/test/{emqx_mgmt_api_routes_SUITE.erl => emqx_mgmt_api_topics_SUITE.erl} (91%) diff --git a/apps/emqx_dashboard/test/emqx_dashboard_bad_api_SUITE.erl b/apps/emqx_dashboard/test/emqx_dashboard_bad_api_SUITE.erl new file mode 100644 index 000000000..341ba5691 --- /dev/null +++ b/apps/emqx_dashboard/test/emqx_dashboard_bad_api_SUITE.erl @@ -0,0 +1,74 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +-module(emqx_dashboard_bad_api_SUITE). + +-compile(nowarn_export_all). +-compile(export_all). + +-include_lib("emqx/include/http_api.hrl"). + +-include_lib("eunit/include/eunit.hrl"). + +-define(SERVER, "http://127.0.0.1:18083/api/v5"). + +all() -> + emqx_common_test_helpers:all(?MODULE). + +init_per_suite(Config) -> + mria:start(), + application:load(emqx_dashboard), + emqx_common_test_helpers:start_apps([emqx_conf, emqx_dashboard], fun set_special_configs/1), + Config. + +set_special_configs(emqx_dashboard) -> + Config = #{ + default_username => <<"admin">>, + default_password => <<"public">>, + listeners => [#{ + protocol => http, + port => 18083 + }] + }, + emqx_config:put([emqx_dashboard], Config), + ok; +set_special_configs(_) -> + ok. + +end_per_suite(Config) -> + end_suite(), + Config. + +end_suite() -> + application:unload(emqx_management), + emqx_common_test_helpers:stop_apps([emqx_dashboard]). + +t_bad_api_path(_) -> + Url = ?SERVER ++ "/for/test/some/path/not/exist", + {error,{"HTTP/1.1", 404, "Not Found"}} = request(Url), + ok. + +request(Url) -> + Request = {Url, []}, + case httpc:request(get, Request, [], []) of + {error, Reason} -> + {error, Reason}; + {ok, {{"HTTP/1.1", Code, _}, _, Return} } + when Code >= 200 andalso Code =< 299 -> + {ok, emqx_json:decode(Return, [return_maps])}; + {ok, {Reason, _, _}} -> + {error, Reason} + end. diff --git a/apps/emqx_management/test/emqx_mgmt_api_routes_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_api_topics_SUITE.erl similarity index 91% rename from apps/emqx_management/test/emqx_mgmt_api_routes_SUITE.erl rename to apps/emqx_management/test/emqx_mgmt_api_topics_SUITE.erl index 460ce55d7..f6536c3b1 100644 --- a/apps/emqx_management/test/emqx_mgmt_api_routes_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_api_topics_SUITE.erl @@ -13,7 +13,7 @@ %% See the License for the specific language governing permissions and %% limitations under the License. %%-------------------------------------------------------------------- --module(emqx_mgmt_api_routes_SUITE). +-module(emqx_mgmt_api_topics_SUITE). -compile(export_all). -compile(nowarn_export_all). @@ -36,7 +36,8 @@ t_nodes_api(_) -> {ok, _} = emqtt:connect(Client), {ok, _, _} = emqtt:subscribe(Client, Topic), - Path = emqx_mgmt_api_test_util:api_path(["routes"]), + %% list all + Path = emqx_mgmt_api_test_util:api_path(["topics"]), {ok, Response} = emqx_mgmt_api_test_util:request_api(get, Path), RoutesData = emqx_json:decode(Response, [return_maps]), Meta = maps:get(<<"meta">>, RoutesData), @@ -48,8 +49,8 @@ t_nodes_api(_) -> ?assertEqual(Topic, maps:get(<<"topic">>, Route)), ?assertEqual(atom_to_binary(node(), utf8), maps:get(<<"node">>, Route)), - %% get routes/:topic - RoutePath = emqx_mgmt_api_test_util:api_path(["routes", Topic]), + %% get topics/:topic + RoutePath = emqx_mgmt_api_test_util:api_path(["topics", Topic]), {ok, RouteResponse} = emqx_mgmt_api_test_util:request_api(get, RoutePath), RouteData = emqx_json:decode(RouteResponse, [return_maps]), ?assertEqual(Topic, maps:get(<<"topic">>, RouteData)),