Kaynağa Gözat

test(auth): authn & authz http placeholder in HTTP path

JimMoen 3 yıl önce
ebeveyn
işleme
c1dfd0aa36

+ 10 - 10
apps/emqx_authn/test/emqx_authn_api_SUITE.erl

@@ -184,14 +184,14 @@ test_authenticators(PathPrefix) ->
         InvalidConfig0
     ),
 
-    InvalidConfig1 = ValidConfig#{
+    ValidConfig1 = ValidConfig#{
         method => <<"get">>,
         headers => #{<<"content-type">> => <<"application/json">>}
     },
-    {ok, 400, _} = request(
-        post,
-        uri(PathPrefix ++ [?CONF_NS]),
-        InvalidConfig1
+    {ok, 200, _} = request(
+        put,
+        uri(PathPrefix ++ [?CONF_NS, "password_based:http"]),
+        ValidConfig1
     ),
 
     ?assertAuthenticatorsMatch(
@@ -264,21 +264,21 @@ test_authenticator(PathPrefix) ->
         InvalidConfig0
     ),
 
-    InvalidConfig1 = ValidConfig0#{
+    ValidConfig1 = ValidConfig0#{
         method => <<"get">>,
         headers => #{<<"content-type">> => <<"application/json">>}
     },
-    {ok, 400, _} = request(
+    {ok, 200, _} = request(
         put,
         uri(PathPrefix ++ [?CONF_NS, "password_based:http"]),
-        InvalidConfig1
+        ValidConfig1
     ),
 
-    ValidConfig1 = ValidConfig0#{pool_size => 9},
+    ValidConfig2 = ValidConfig0#{pool_size => 9},
     {ok, 200, _} = request(
         put,
         uri(PathPrefix ++ [?CONF_NS, "password_based:http"]),
-        ValidConfig1
+        ValidConfig2
     ),
 
     {ok, 404, _} = request(

+ 48 - 0
apps/emqx_authz/test/emqx_authz_http_SUITE.erl

@@ -201,6 +201,54 @@ t_query_params(_Config) ->
         emqx_access_control:authorize(ClientInfo, publish, <<"t">>)
     ).
 
+t_path(_Config) ->
+    ok = setup_handler_and_config(
+        fun(Req0, State) ->
+            ?assertEqual(
+                <<
+                    "/authz/users/"
+                    "user%20name/"
+                    "client%20id/"
+                    "127.0.0.1/"
+                    "MQTT/"
+                    "MOUNTPOINT/"
+                    "t/1/"
+                    "publish"
+                >>,
+                cowboy_req:path(Req0)
+            ),
+            Req = cowboy_req:reply(200, Req0),
+            {ok, Req, State}
+        end,
+        #{
+            <<"url">> => <<
+                "http://127.0.0.1:33333/authz/users/"
+                "${username}/"
+                "${clientid}/"
+                "${peerhost}/"
+                "${proto_name}/"
+                "${mountpoint}/"
+                "${topic}/"
+                "${action}"
+            >>
+        }
+    ),
+
+    ClientInfo = #{
+        clientid => <<"client id">>,
+        username => <<"user name">>,
+        peerhost => {127, 0, 0, 1},
+        protocol => <<"MQTT">>,
+        mountpoint => <<"MOUNTPOINT">>,
+        zone => default,
+        listener => {tcp, default}
+    },
+
+    ?assertEqual(
+        allow,
+        emqx_access_control:authorize(ClientInfo, publish, <<"t/1">>)
+    ).
+
 t_json_body(_Config) ->
     ok = setup_handler_and_config(
         fun(Req0, State) ->