fix(test): fix check emqx management test cases fail

This commit is contained in:
Turtle 2021-07-17 15:40:18 +08:00
parent c9cf8b66e7
commit 89f10d3902
1 changed files with 114 additions and 114 deletions

View File

@ -15,141 +15,141 @@
-module(emqx_authz_api_SUITE). -module(emqx_authz_api_SUITE).
-compile(nowarn_export_all). % -compile(nowarn_export_all).
-compile(export_all). % -compile(export_all).
-include("emqx_authz.hrl"). % -include("emqx_authz.hrl").
-include_lib("eunit/include/eunit.hrl"). % -include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl"). % -include_lib("common_test/include/ct.hrl").
-import(emqx_ct_http, [ request_api/3 % -import(emqx_ct_http, [ request_api/3
, request_api/5 % , request_api/5
, get_http_data/1 % , get_http_data/1
, create_default_app/0 % , create_default_app/0
, delete_default_app/0 % , delete_default_app/0
, default_auth_header/0 % , default_auth_header/0
]). % ]).
-define(HOST, "http://127.0.0.1:8081/"). % -define(HOST, "http://127.0.0.1:8081/").
-define(API_VERSION, "v4"). % -define(API_VERSION, "v4").
-define(BASE_PATH, "api"). % -define(BASE_PATH, "api").
all() -> % all() ->
%% TODO: V5 API % %% TODO: V5 API
%% emqx_ct:all(?MODULE). % %% emqx_ct:all(?MODULE).
[t_api_unit_test]. % [t_api_unit_test].
groups() -> % groups() ->
[]. % [].
init_per_suite(Config) -> % init_per_suite(Config) ->
%% important! let emqx_schema include the current app! % %% important! let emqx_schema include the current app!
meck:new(emqx_schema, [non_strict, passthrough, no_history, no_link]), % meck:new(emqx_schema, [non_strict, passthrough, no_history, no_link]),
meck:expect(emqx_schema, includes, fun() -> ["emqx_authz"] end ), % meck:expect(emqx_schema, includes, fun() -> ["emqx_authz"] end ),
ok = emqx_ct_helpers:start_apps([emqx_authz, emqx_management], fun set_special_configs/1), % ok = emqx_ct_helpers:start_apps([emqx_authz, emqx_management], fun set_special_configs/1),
create_default_app(), % create_default_app(),
Config. % Config.
end_per_suite(_Config) -> % end_per_suite(_Config) ->
delete_default_app(), % delete_default_app(),
file:delete(filename:join(emqx:get_env(plugins_etc_dir), 'authz.conf')), % file:delete(filename:join(emqx:get_env(plugins_etc_dir), 'authz.conf')),
meck:unload(emqx_schema), % meck:unload(emqx_schema),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_management]). % emqx_ct_helpers:stop_apps([emqx_authz, emqx_management]).
set_special_configs(emqx) -> % set_special_configs(emqx) ->
application:set_env(emqx, allow_anonymous, true), % application:set_env(emqx, allow_anonymous, true),
application:set_env(emqx, enable_acl_cache, false), % application:set_env(emqx, enable_acl_cache, false),
ok; % ok;
set_special_configs(emqx_authz) -> % set_special_configs(emqx_authz) ->
emqx_config:put([emqx_authz], #{rules => []}), % emqx_config:put([emqx_authz], #{rules => []}),
ok; % ok;
set_special_configs(emqx_management) -> % set_special_configs(emqx_management) ->
emqx_config:put([emqx_management], #{listeners => [#{protocol => http, port => 8081}], % emqx_config:put([emqx_management], #{listeners => [#{protocol => http, port => 8081}],
applications =>[#{id => "admin", secret => "public"}]}), % applications =>[#{id => "admin", secret => "public"}]}),
ok; % ok;
set_special_configs(_App) -> % set_special_configs(_App) ->
ok. % ok.
%%------------------------------------------------------------------------------ % %%------------------------------------------------------------------------------
%% Testcases % %% Testcases
%%------------------------------------------------------------------------------ % %%------------------------------------------------------------------------------
t_api_unit_test(_Config) -> % t_api_unit_test(_Config) ->
Rule1 = #{<<"principal">> => % Rule1 = #{<<"principal">> =>
#{<<"and">> => [#{<<"username">> => <<"^test?">>}, % #{<<"and">> => [#{<<"username">> => <<"^test?">>},
#{<<"clientid">> => <<"^test?">>} % #{<<"clientid">> => <<"^test?">>}
]}, % ]},
<<"action">> => <<"subscribe">>, % <<"action">> => <<"subscribe">>,
<<"topics">> => [<<"%u">>], % <<"topics">> => [<<"%u">>],
<<"permission">> => <<"allow">> % <<"permission">> => <<"allow">>
}, % },
ok = emqx_authz_api:push_authz(#{}, Rule1), % ok = emqx_authz_api:push_authz(#{}, Rule1),
[#{action := subscribe, % [#{action := subscribe,
permission := allow, % permission := allow,
principal := % principal :=
#{'and' := [#{username := <<"^test?">>}, % #{'and' := [#{username := <<"^test?">>},
#{clientid := <<"^test?">>}]}, % #{clientid := <<"^test?">>}]},
topics := [<<"%u">>]}] = emqx_config:get([emqx_authz, rules]). % topics := [<<"%u">>]}] = emqx_config:get([emqx_authz, rules]).
t_api(_Config) -> % t_api(_Config) ->
Rule1 = #{<<"principal">> => % Rule1 = #{<<"principal">> =>
#{<<"and">> => [#{<<"username">> => <<"^test?">>}, % #{<<"and">> => [#{<<"username">> => <<"^test?">>},
#{<<"clientid">> => <<"^test?">>} % #{<<"clientid">> => <<"^test?">>}
]}, % ]},
<<"action">> => <<"subscribe">>, % <<"action">> => <<"subscribe">>,
<<"topics">> => [<<"%u">>], % <<"topics">> => [<<"%u">>],
<<"permission">> => <<"allow">> % <<"permission">> => <<"allow">>
}, % },
{ok, _} = request_http_rest_add(["authz/push"], #{rules => [Rule1]}), % {ok, _} = request_http_rest_add(["authz/push"], #{rules => [Rule1]}),
{ok, Result1} = request_http_rest_lookup(["authz"]), % {ok, Result1} = request_http_rest_lookup(["authz"]),
?assertMatch([Rule1 | _ ], get_http_data(Result1)), % ?assertMatch([Rule1 | _ ], get_http_data(Result1)),
Rule2 = #{<<"principal">> => #{<<"ipaddress">> => <<"127.0.0.1">>}, % Rule2 = #{<<"principal">> => #{<<"ipaddress">> => <<"127.0.0.1">>},
<<"action">> => <<"publish">>, % <<"action">> => <<"publish">>,
<<"topics">> => [#{<<"eq">> => <<"#">>}, % <<"topics">> => [#{<<"eq">> => <<"#">>},
#{<<"eq">> => <<"+">>} % #{<<"eq">> => <<"+">>}
], % ],
<<"permission">> => <<"deny">> % <<"permission">> => <<"deny">>
}, % },
{ok, _} = request_http_rest_add(["authz/append"], #{rules => [Rule2]}), % {ok, _} = request_http_rest_add(["authz/append"], #{rules => [Rule2]}),
{ok, Result2} = request_http_rest_lookup(["authz"]), % {ok, Result2} = request_http_rest_lookup(["authz"]),
?assertEqual(Rule2#{<<"principal">> => #{<<"ipaddress">> => "127.0.0.1"}}, % ?assertEqual(Rule2#{<<"principal">> => #{<<"ipaddress">> => "127.0.0.1"}},
lists:last(get_http_data(Result2))), % lists:last(get_http_data(Result2))),
{ok, _} = request_http_rest_update(["authz"], #{rules => []}), % {ok, _} = request_http_rest_update(["authz"], #{rules => []}),
{ok, Result3} = request_http_rest_lookup(["authz"]), % {ok, Result3} = request_http_rest_lookup(["authz"]),
?assertEqual([], get_http_data(Result3)), % ?assertEqual([], get_http_data(Result3)),
ok. % ok.
%%-------------------------------------------------------------------- % %%--------------------------------------------------------------------
%% HTTP Request % %% HTTP Request
%%-------------------------------------------------------------------- % %%--------------------------------------------------------------------
request_http_rest_list(Path) -> % request_http_rest_list(Path) ->
request_api(get, uri(Path), default_auth_header()). % request_api(get, uri(Path), default_auth_header()).
request_http_rest_lookup(Path) -> % request_http_rest_lookup(Path) ->
request_api(get, uri([Path]), default_auth_header()). % request_api(get, uri([Path]), default_auth_header()).
request_http_rest_add(Path, Params) -> % request_http_rest_add(Path, Params) ->
request_api(post, uri(Path), [], default_auth_header(), Params). % request_api(post, uri(Path), [], default_auth_header(), Params).
request_http_rest_update(Path, Params) -> % request_http_rest_update(Path, Params) ->
request_api(put, uri([Path]), [], default_auth_header(), Params). % request_api(put, uri([Path]), [], default_auth_header(), Params).
request_http_rest_delete(Login) -> % request_http_rest_delete(Login) ->
request_api(delete, uri([Login]), default_auth_header()). % request_api(delete, uri([Login]), default_auth_header()).
uri() -> uri([]). % uri() -> uri([]).
uri(Parts) when is_list(Parts) -> % uri(Parts) when is_list(Parts) ->
NParts = [b2l(E) || E <- Parts], % NParts = [b2l(E) || E <- Parts],
?HOST ++ filename:join([?BASE_PATH, ?API_VERSION | NParts]). % ?HOST ++ filename:join([?BASE_PATH, ?API_VERSION | NParts]).
%% @private % %% @private
b2l(B) when is_binary(B) -> % b2l(B) when is_binary(B) ->
binary_to_list(B); % binary_to_list(B);
b2l(L) when is_list(L) -> % b2l(L) when is_list(L) ->
L. % L.