anchor.diff:  -module(builderl_github_webhook_test).
  +-compile({parse_transform, lager_transform}).
  +-compile(export_all).
   
   -include_lib("eunit/include/eunit.hrl").
   
  --compile(export_all).
   
   -define(MUT, builderl_github_webhook).
   
  
positive.code:     {setup,
       fun setup_handler/0,
       fun teardown_handler/1,
       fun invalid_signature/1}.
  
  setup_handler() ->
      application:ensure_all_started(lager),
  
negative.code:     [?_assertEqual(Ref, RRef),
       ?_assertEqual(<<"Invalid signature">>, Body)].
  
  no_signature_configured_not_whitelisted(_Config) ->
      State0 = ?MUT:state_init(),
      PostReq = #{method => <<"POST">>} ,
  
      Data = #{<<"repository">> =>
                  #{<<"url">> => <<"repo_url">>,
                    <<"full_name">> => <<"test_full_name">>
                  },
               <<"ref">> => <<"test_ref">>,
               <<"after">> => <<"after_sha">>
      },
  
      ok = meck:expect(cowboy_req, read_body, fun(Req) -> make_json_response(Data, Req) end),
      ok = meck:expect(cowboy_req, header, fun(_, _) -> undefined end),
  
      Ref = make_ref(),
      ok = meck:expect(cowboy_req, reply, fun(400, _, Body, _Req) -> {Ref, Body} end),
      {ok, {RRef, Body}, _} = ?MUT:init(PostReq, State0),
      DoesMatch = re_match(Body, ".*not in the builderl whitelist.*"),
      [?_assertEqual(Ref, RRef),
       ?_assert(DoesMatch)].
  
  
  make_json_response(Data, Req) ->
      Json = jsx:encode(Data),
      {ok, Json, Req}.
  
  re_match(Subject, RE) ->
      case re:run(Subject, RE) of
          nomatch -> ok = lager:info("~p does not match ~p", [RE, Subject]),
                     false;
          {match, _} -> true
      end.
  