fix: dashboard ct failed

This commit is contained in:
Zhongwen Deng 2022-04-15 13:43:26 +08:00
parent 164754b182
commit f98cb972e2
6 changed files with 82 additions and 51 deletions

View File

@ -27,6 +27,7 @@
-export([ -export([
init_i18n/2, init_i18n/2,
init_i18n/0,
get_i18n/0, get_i18n/0,
clear_i18n/0 clear_i18n/0
]). ]).

View File

@ -197,7 +197,7 @@ cors(_) ->
i18n_lang(type) -> ?ENUM([en, zh]); i18n_lang(type) -> ?ENUM([en, zh]);
i18n_lang(default) -> zh; i18n_lang(default) -> zh;
i18n_lang('readOnly') -> true; i18n_lang('readOnly') -> true;
i18n_lang(desc) -> "i18n language"; i18n_lang(desc) -> "Internationalization language support.";
i18n_lang(_) -> undefined. i18n_lang(_) -> undefined.
sc(Type, Meta) -> hoconsc:mk(Type, Meta). sc(Type, Meta) -> hoconsc:mk(Type, Meta).

View File

@ -306,7 +306,7 @@ check_request_body(#{body := Body}, Spec, _Module, _CheckFun, false) when is_map
%% tags, description, summary, security, deprecated %% tags, description, summary, security, deprecated
meta_to_spec(Meta, Module, Options) -> meta_to_spec(Meta, Module, Options) ->
{Params, Refs1} = parameters(maps:get(parameters, Meta, []), Module), {Params, Refs1} = parameters(maps:get(parameters, Meta, []), Module),
{RequestBody, Refs2} = request_body(maps:get('requestBody', Meta, []), Module), {RequestBody, Refs2} = request_body(maps:get('requestBody', Meta, []), Module, Options),
{Responses, Refs3} = responses(maps:get(responses, Meta, #{}), Module, Options), {Responses, Refs3} = responses(maps:get(responses, Meta, #{}), Module, Options),
{ {
generate_method_desc(to_spec(Meta, Params, RequestBody, Responses)), generate_method_desc(to_spec(Meta, Params, RequestBody, Responses)),
@ -424,11 +424,11 @@ resolve_desc(Key, Struct) ->
false -> Desc false -> Desc
end. end.
request_body(#{content := _} = Content, _Module) -> request_body(#{content := _} = Content, _Module, _Options) ->
{Content, []}; {Content, []};
request_body([], _Module) -> request_body([], _Module, _Options) ->
{[], []}; {[], []};
request_body(Schema, Module) -> request_body(Schema, Module, Options) ->
{{Props, Refs}, Examples} = {{Props, Refs}, Examples} =
case hoconsc:is_schema(Schema) of case hoconsc:is_schema(Schema) of
true -> true ->
@ -436,7 +436,7 @@ request_body(Schema, Module) ->
SchemaExamples = hocon_schema:field_schema(Schema, examples), SchemaExamples = hocon_schema:field_schema(Schema, examples),
{hocon_schema_to_spec(HoconSchema, Module), SchemaExamples}; {hocon_schema_to_spec(HoconSchema, Module), SchemaExamples};
false -> false ->
{parse_object(Schema, Module, #{}), undefined} {parse_object(Schema, Module, Options), undefined}
end, end,
{#{<<"content">> => content(Props, Examples)}, Refs}. {#{<<"content">> => content(Props, Examples)}, Refs}.

View File

@ -4,6 +4,7 @@
%% API %% API
-export([paths/0, api_spec/0, schema/1, fields/1]). -export([paths/0, api_spec/0, schema/1, fields/1]).
-export([init_per_suite/1, end_per_suite/1]).
-export([t_in_path/1, t_in_query/1, t_in_mix/1, t_without_in/1, t_ref/1, t_public_ref/1]). -export([t_in_path/1, t_in_query/1, t_in_mix/1, t_without_in/1, t_ref/1, t_public_ref/1]).
-export([t_require/1, t_nullable/1, t_method/1, t_api_spec/1]). -export([t_require/1, t_nullable/1, t_method/1, t_api_spec/1]).
-export([t_in_path_trans/1, t_in_query_trans/1, t_in_mix_trans/1, t_ref_trans/1]). -export([t_in_path_trans/1, t_in_query_trans/1, t_in_mix_trans/1, t_ref_trans/1]).
@ -26,6 +27,27 @@ groups() -> [
t_in_path_trans_error, t_in_query_trans_error, t_in_mix_trans_error]} t_in_path_trans_error, t_in_query_trans_error, t_in_mix_trans_error]}
]. ].
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),
emqx_dashboard:init_i18n(),
Config.
set_special_configs(emqx_dashboard) ->
emqx_dashboard_api_test_helpers:set_default_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_in_path(_Config) -> t_in_path(_Config) ->
Expect = Expect =
[#{description => <<"Indicates which sorts of issues to return">>, [#{description => <<"Indicates which sorts of issues to return">>,

View File

@ -3,41 +3,36 @@
-behaviour(minirest_api). -behaviour(minirest_api).
-behaviour(hocon_schema). -behaviour(hocon_schema).
%% API -compile(nowarn_export_all).
-export([paths/0, api_spec/0, schema/1, fields/1]). -compile(export_all).
-export([t_object/1, t_nest_object/1, t_api_spec/1,
t_local_ref/1, t_remote_ref/1, t_bad_ref/1, t_none_ref/1, t_nest_ref/1,
t_ref_array_with_key/1, t_ref_array_without_key/1, t_sub_fields/1
]).
-export([
t_object_trans/1, t_object_notrans/1, t_nest_object_trans/1, t_local_ref_trans/1,
t_remote_ref_trans/1, t_nest_ref_trans/1,
t_ref_array_with_key_trans/1, t_ref_array_without_key_trans/1,
t_ref_trans_error/1, t_object_trans_error/1
]).
-export([all/0, suite/0, groups/0]).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-include_lib("typerefl/include/types.hrl"). -include_lib("typerefl/include/types.hrl").
-include_lib("hocon/include/hoconsc.hrl"). -include_lib("hocon/include/hoconsc.hrl").
-import(hoconsc, [mk/2]). -import(hoconsc, [mk/2]).
all() -> [{group, spec}, {group, validation}]. all() -> emqx_common_test_helpers:all(?MODULE).
suite() -> [{timetrap, {minutes, 1}}]. init_per_suite(Config) ->
groups() -> [ mria:start(),
{spec, [parallel], [ application:load(emqx_dashboard),
t_api_spec, t_object, t_nest_object, emqx_common_test_helpers:start_apps([emqx_conf, emqx_dashboard], fun set_special_configs/1),
t_local_ref, t_remote_ref, t_bad_ref, t_none_ref, emqx_dashboard:init_i18n(),
t_ref_array_with_key, t_ref_array_without_key, t_nest_ref]}, Config.
{validation, [parallel],
[ set_special_configs(emqx_dashboard) ->
t_object_trans, t_object_notrans, t_local_ref_trans, t_remote_ref_trans, emqx_dashboard_api_test_helpers:set_default_config(),
t_ref_array_with_key_trans, t_ref_array_without_key_trans, t_nest_ref_trans, ok;
t_ref_trans_error, t_object_trans_error set_special_configs(_) ->
%% t_nest_object_trans, ok.
]}
]. end_per_suite(Config) ->
end_suite(),
Config.
end_suite() ->
application:unload(emqx_management),
emqx_common_test_helpers:stop_apps([emqx_dashboard]).
t_object(_Config) -> t_object(_Config) ->
Spec = #{ Spec = #{
@ -281,7 +276,7 @@ t_object_notrans(_Config) ->
?assertEqual(Body, ActualBody), ?assertEqual(Body, ActualBody),
ok. ok.
t_nest_object_trans(_Config) -> todo_t_nest_object_check(_Config) ->
Path = "/nest/object", Path = "/nest/object",
Body = #{ Body = #{
<<"timeout">> => "10m", <<"timeout">> => "10m",
@ -306,7 +301,7 @@ t_nest_object_trans(_Config) ->
body => #{<<"per_page">> => 10, body => #{<<"per_page">> => 10,
<<"timeout">> => 600} <<"timeout">> => 600}
}, },
{ok, NewRequest} = trans_requestBody(Path, Body), {ok, NewRequest} = check_requestBody(Path, Body),
?assertEqual(Expect, NewRequest), ?assertEqual(Expect, NewRequest),
ok. ok.
@ -487,6 +482,10 @@ trans_requestBody(Path, Body) ->
trans_requestBody(Path, Body, trans_requestBody(Path, Body,
fun emqx_dashboard_swagger:filter_check_request_and_translate_body/2). fun emqx_dashboard_swagger:filter_check_request_and_translate_body/2).
check_requestBody(Path, Body) ->
trans_requestBody(Path, Body,
fun emqx_dashboard_swagger:filter_check_request/2).
trans_requestBody(Path, Body, Filter) -> trans_requestBody(Path, Body, Filter) ->
Meta = #{module => ?MODULE, method => post, path => Path}, Meta = #{module => ?MODULE, method => post, path => Path},
Request = #{bindings => #{}, query_string => #{}, body => Body}, Request = #{bindings => #{}, query_string => #{}, body => Body},

View File

@ -10,22 +10,31 @@
-include_lib("hocon/include/hoconsc.hrl"). -include_lib("hocon/include/hoconsc.hrl").
-import(hoconsc, [mk/2]). -import(hoconsc, [mk/2]).
-export([all/0, suite/0, groups/0]). -compile(nowarn_export_all).
-export([paths/0, api_spec/0, schema/1, fields/1]). -compile(export_all).
-export([t_simple_binary/1, t_object/1, t_nest_object/1, t_empty/1, t_error/1,
t_raw_local_ref/1, t_raw_remote_ref/1, t_hocon_schema_function/1, t_complicated_type/1,
t_local_ref/1, t_remote_ref/1, t_bad_ref/1, t_none_ref/1, t_nest_ref/1, t_sub_fields/1,
t_ref_array_with_key/1, t_ref_array_without_key/1, t_api_spec/1]).
all() -> [{group, spec}]. all() -> emqx_common_test_helpers:all(?MODULE).
suite() -> [{timetrap, {minutes, 1}}].
groups() -> [ init_per_suite(Config) ->
{spec, [parallel], [ mria:start(),
t_api_spec, t_simple_binary, t_object, t_nest_object, t_error, t_complicated_type, application:load(emqx_dashboard),
t_raw_local_ref, t_raw_remote_ref, t_empty, t_hocon_schema_function, emqx_common_test_helpers:start_apps([emqx_conf, emqx_dashboard], fun set_special_configs/1),
t_local_ref, t_remote_ref, t_bad_ref, t_none_ref, t_sub_fields, emqx_dashboard:init_i18n(),
t_ref_array_with_key, t_ref_array_without_key, t_nest_ref]} Config.
].
set_special_configs(emqx_dashboard) ->
emqx_dashboard_api_test_helpers:set_default_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_simple_binary(_config) -> t_simple_binary(_config) ->
Path = "/simple/bin", Path = "/simple/bin",