File size: 1,550 Bytes
c9c49ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
defmodule PlausibleWeb.CaptchaTest do
  use Plausible.DataCase

  import Mox
  setup :verify_on_exit!

  alias PlausibleWeb.Captcha

  describe "mocked payloads" do
    @failure Jason.decode!(~s/{"success":false,"error-codes":["invalid-input-response"]}/)
    @success Jason.decode!(~s/{"success":true}/)

    test "returns false for non-success response" do
      expect(
        Plausible.HTTPClient.Mock,
        :post,
        fn "https://hcaptcha.com/siteverify",
           [{"content-type", "application/x-www-form-urlencoded"}],
           %{response: "bad", secret: "scottiger"} ->
          {:ok,
           %Finch.Response{
             status: 200,
             headers: [{"content-type", "application/json"}],
             body: @failure
           }}
        end
      )

      refute Captcha.verify("bad")
    end

    test "returns true for successful response" do
      expect(
        Plausible.HTTPClient.Mock,
        :post,
        fn "https://hcaptcha.com/siteverify",
           [{"content-type", "application/x-www-form-urlencoded"}],
           %{response: "good", secret: "scottiger"} ->
          {:ok,
           %Finch.Response{
             status: 200,
             headers: [{"content-type", "application/json"}],
             body: @success
           }}
        end
      )

      assert Captcha.verify("good")
    end
  end

  describe "with patched application env" do
    setup_patch_env(:hcaptcha, sitekey: nil)

    test "returns true when disabled" do
      assert Captcha.verify("disabled")
    end
  end
end