<commit_before>-module(webhooks_handler).
-include("vmq_webhooks_test.hrl").

-export([init/3]).
-export([handle/2]).
-export([terminate/3]).

-export([start_endpoint/0,
         stop_endpoint/0]).

start_endpoint() ->
    Dispatch = cowboy_router:compile(
                 [{'_', [{"/", ?MODULE, []}]}]),
    {ok, _} = cowboy:start_http(http, 1, [{port, 34567}],
        [{env, [{dispatch, Dispatch}]}]
    ).

stop_endpoint() ->
    cowboy:stop_listener(http).

%% Cowboy callbacks
init(_Type, Req, []) ->
	{ok, Req, undefined}.

handle(Req, State) ->
    {Hook, Req2} = cowboy_req:header(<<"vernemq-hook">>, Req),
    {ok, Body, Req3} = cowboy_req:body(Req2),
    {Code, Resp} = process_hook(Hook, jsx:decode(Body, [{labels, atom}, return_maps])),
    {ok, Req4} =
        cowboy_req:reply(Code,
                         [
                          {<<"content-type">>, <<"text/json">>}
                         ], jsx:encode(Resp), Req3),
    {ok, Req4, State}.

%% callbacks for each hook
auth_on_register(#{peer_addr := ?PEER_BIN,
                   peer_port := ?PEERPORT,
                   client_id := <<"undefined_creds">>,
                   mountpoint := ?MOUNTPOINT_BIN,
                   username := null,
                   password := <PASSWORD>,
                   clean_session := true
                 }) ->
    {200, #{result => <<"ok">>}};
auth_on_register(#{peer_addr := ?PEER_BIN,
                   peer_port := ?PEERPORT,
                   client_id := ?ALLOWED_CLIENT_ID,
                   mountpoint := ?MOUNTPOINT_BIN,
                   username := ?USERNAME,
                   password := <PASSWORD>,
                   clean_session := true
                 }) ->
    {200, #{result => <<"ok">>}};
auth_on_register(#{client_id := ?NOT_ALLOWED_CLIENT_ID}) ->
    {200, #{result => #{error => <<"not_allowed">>}}};
auth_on_register(#{client_id := ?IGNORED_CLIENT_ID}) ->
    {200, #{result => <<"next">>}};
auth_on_register(#{client_id := ?CHANGED_CLIENT_ID}) ->
    {200, #{result => <<"ok">>,
            modifiers => #{mountpoint => <<"mynewmount">>}}};
auth_on_register(#{subscriberid := <<"internal_server_error">>}) ->
    throw(internal_server_error).

auth_on_publish(#{username := ?USERNAME,
                  client_id := ?ALLOWED_CLIENT_ID,
                  mountpoint := ?MOUNTPOINT_BIN,
                  qos := 1,
                  topic := ?TOPIC,
                  payload := ?PAYLOAD,
                  retain := false
                 }) ->
    {200, #{result => <<"ok">>}};
auth_on_publish(#{username := ?USERNAME,
                  client_id := ?BASE64_PAYLOAD_CLIENT_ID,
                  mountpoint := ?MOUNTPOINT_BIN,
                  qos := 1,
                  topic := ?TOPIC,
                  payload := Base64Payload,
                  retain := false
                 }) ->
    ?PAYLOAD = base64:decode(Base64Payload),<commit_msg>Fix changed_client_id test<commit_after>-module(webhooks_handler).
-include("vmq_webhooks_test.hrl").

-export([init/3]).
-export([handle/2]).
-export([terminate/3]).

-export([start_endpoint/0,
         stop_endpoint/0]).

start_endpoint() ->
    Dispatch = cowboy_router:compile(
                 [{'_', [{"/", ?MODULE, []}]}]),
    {ok, _} = cowboy:start_http(http, 1, [{port, 34567}],
        [{env, [{dispatch, Dispatch}]}]
    ).

stop_endpoint() ->
    cowboy:stop_listener(http).

%% Cowboy callbacks
init(_Type, Req, []) ->
	{ok, Req, undefined}.

handle(Req, State) ->
    {Hook, Req2} = cowboy_req:header(<<"vernemq-hook">>, Req),
    {ok, Body, Req3} = cowboy_req:body(Req2),
    {Code, Resp} = process_hook(Hook, jsx:decode(Body, [{labels, atom}, return_maps])),
    {ok, Req4} =
        cowboy_req:reply(Code,
                         [
                          {<<"content-type">>, <<"text/json">>}
                         ], jsx:encode(Resp), Req3),
    {ok, Req4, State}.

%% callbacks for each hook
auth_on_register(#{peer_addr := ?PEER_BIN,
                   peer_port := ?PEERPORT,
                   client_id := <<"undefined_creds">>,
                   mountpoint := ?MOUNTPOINT_BIN,
                   username := null,
                   password := <PASSWORD>,
                   clean_session := true
                 }) ->
    {200, #{result => <<"ok">>}};
auth_on_register(#{peer_addr := ?PEER_BIN,
                   peer_port := ?PEERPORT,
                   client_id := ?ALLOWED_CLIENT_ID,
                   mountpoint := ?MOUNTPOINT_BIN,
                   username := ?USERNAME,
                   password := <PASSWORD>,
                   clean_session := true
                 }) ->
    {200, #{result => <<"ok">>}};
auth_on_register(#{client_id := ?NOT_ALLOWED_CLIENT_ID}) ->
    {200, #{result => #{error => <<"not_allowed">>}}};
auth_on_register(#{client_id := ?IGNORED_CLIENT_ID}) ->
    {200, #{result => <<"next">>}};
auth_on_register(#{client_id := ?CHANGED_CLIENT_ID}) ->
    {200, #{result => <<"ok">>,
            modifiers => #{mountpoint => <<"mynewmount">>,
                           client_id => <<"changed_client_id">>}}};
auth_on_register(#{subscriberid := <<"internal_server_error">>}) ->
    throw(internal_server_error).

auth_on_publish(#{username := ?USERNAME,
                  client_id := ?ALLOWED_CLIENT_ID,
                  mountpoint := ?MOUNTPOINT_BIN,
                  qos := 1,
                  topic := ?TOPIC,
                  payload := ?PAYLOAD,
                  retain := false
                 }) ->
    {200, #{result => <<"ok">>}};
auth_on_publish(#{username := ?USERNAME,
                  client_id := ?BASE64_PAYLOAD_CLIENT_ID,
                  mountpoint := ?MOUNTPOINT_BIN,
                  qos := 1,
                  topic := ?TOPIC,
                  payload := Base64Payload,
                  retain := false
                 }) ->
    ?PAYLOAD = base64:decode(Base64Payload),
{'fraction_non_alphanumeric': 0.17427597955706986, 'fraction_numerical': 0.015332197614991482, 'mean_word_length': 1.9487694625816172, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4861045', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1856, 'n_words': 299}