aboutsummaryrefslogtreecommitdiff
path: root/test/recaptcha_test.exs
blob: ad57772a6705e7a9d4e7a9e37aaafc9c430f0c53 (plain)
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
defmodule RecaptchaTest do
  use ExUnit.Case, async: true

  # see https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha-v2-what-should-i-do
  @google_test_secret "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"

  test "When the supplied g-recaptcha-response is nil, :invalid_input_response is returned" do
    assert {:error, messages} = Recaptcha.verify(nil)
    assert messages == [:invalid_input_response]
  end

  test "When the supplied g-recaptcha-response is not nil, but invalid, multiple errors are returned" do
    assert {:error, messages} = Recaptcha.verify("not_valid")
    assert messages == [:invalid_input_response, :invalid_input_secret]
  end

  test "When the correct secret is supplied but g-recaptcha-response is invalid :invalid_input_response' is returned" do
    assert {:error, messages} = Recaptcha.verify("not_valid", secret: @google_test_secret)
    assert messages == [:invalid_input_response]
  end

  test "When a valid response is supplied, a success response is returned" do
    assert {:ok, %{challenge_ts: _, hostname: _}} = Recaptcha.verify("valid_response", secret: "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe")
  end

  test "When secret is not overridden the configured secret is used" do
    Recaptcha.verify("valid_response")

    assert_received {:request_verification, "response=valid_response&secret=test_secret", _}
  end

  test "When the timeout is overridden that config is passed to verify/2 as an option" do
    Recaptcha.verify("valid_response", timeout: 25000)

    assert_received {:request_verification, _, [timeout: 25000]}
  end

  test "Remote IP is used in the request body when it is passed into verify/2 as an option" do
    Recaptcha.verify("valid_response", remote_ip: "192.168.1.1")

    assert_received {:request_verification, "response=valid_response&secret=test_secret&remote_ip=192.168.1.1", _}
  end

  test "Adding unsupported options does not append them to the request body" do
    Recaptcha.verify("valid_response", unsupported_option: "not_valid")

    assert_received {:request_verification, "response=valid_response&secret=test_secret", _}
  end
end