Browse Source

Add more test cases for emqx_access_control

zhanghongtong 6 years ago
parent
commit
53caec8504
1 changed files with 39 additions and 9 deletions
  1. 39 9
      test/emqx_access_control_SUITE.erl

+ 39 - 9
test/emqx_access_control_SUITE.erl

@@ -19,22 +19,52 @@
 -compile(export_all).
 -compile(export_all).
 -compile(nowarn_export_all).
 -compile(nowarn_export_all).
 
 
+-include("emqx_mqtt.hrl").
 -include_lib("eunit/include/eunit.hrl").
 -include_lib("eunit/include/eunit.hrl").
 
 
 all() -> emqx_ct:all(?MODULE).
 all() -> emqx_ct:all(?MODULE).
 
 
-init_per_testcase(_TestCase, Config) ->
+init_per_suite(Config) ->
+    emqx_ct_helpers:boot_modules([router, broker]),
+    emqx_ct_helpers:start_apps([]),
     Config.
     Config.
 
 
-end_per_testcase(_TestCase, Config) ->
-    Config.
+end_per_suite(_Config) ->
+    emqx_ct_helpers:stop_apps([]).
+
+t_authenticate(_) ->
+    emqx_zone:set_env(zone, allow_anonymous, false),
+    ?assertMatch({error, _}, emqx_access_control:authenticate(clientinfo())),
+    emqx_zone:set_env(zone, allow_anonymous, true),
+    ?assertMatch({ok, _}, emqx_access_control:authenticate(clientinfo())).
+
+t_check_acl(_) ->
+    emqx_zone:set_env(zone, acl_nomatch, deny),
+    application:set_env(emqx, enable_acl_cache, false),
+    Publish = ?PUBLISH_PACKET(?QOS_0, <<"t">>, 1, <<"payload">>),
+    ?assertEqual(deny, emqx_access_control:check_acl(clientinfo(), Publish, <<"t">>)),
 
 
-% t_authenticate(_) ->
-%     error('TODO').
+    emqx_zone:set_env(zone, acl_nomatch, allow),
+    application:set_env(emqx, enable_acl_cache, true),
+    Publish = ?PUBLISH_PACKET(?QOS_0, <<"t">>, 1, <<"payload">>),
+    ?assertEqual(allow, emqx_access_control:check_acl(clientinfo(), Publish, <<"t">>)).
 
 
-% t_check_acl(_) ->
-%     error('TODO').
+t_reload_acl(_) ->
+    ?assertEqual(ok, emqx_access_control:reload_acl()).
 
 
-% t_reload_acl(_) ->
-%     error('TODO').
+%%--------------------------------------------------------------------
+%% Helper functions
+%%--------------------------------------------------------------------
 
 
+clientinfo() -> clientinfo(#{}).
+clientinfo(InitProps) ->
+    maps:merge(#{zone       => zone,
+                 protocol   => mqtt,
+                 peerhost   => {127,0,0,1},
+                 clientid   => <<"clientid">>,
+                 username   => <<"username">>,
+                 password   => <<"passwd">>,
+                 is_superuser => false,
+                 peercert   => undefined,
+                 mountpoint => undefined
+                }, InitProps).